package com.ruoyi.projects.apis;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ruoyi.common.annotation.IgnoreAuth;
import com.ruoyi.common.annotation.LoginUserInfo;
import com.ruoyi.common.constant.HttpStatus;
import com.ruoyi.common.core.controller.ApiBaseController;
import com.ruoyi.common.core.domain.ApiResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.projects.Vos.WxUserInfoDTO;
import com.ruoyi.projects.domain.SlActivitys;
import com.ruoyi.projects.service.ISlActivitysService;
import com.ruoyi.projects.service.ISlProductService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 订单预约相关接口
 */
@RestController
@RequestMapping("/api/orders")
public class OrderApis extends ApiBaseController {

    private static final Logger log = LoggerFactory.getLogger(OrderApis.class);

    @Autowired
    private ISlProductService slProductService;

    @Autowired
    private ISlActivitysService slActivitysService;

    /**
     * 活动页面分页查询展示接口
     */
    @PostMapping("/slActivityList")
    public TableDataInfo slActivityList(@LoginUserInfo WxUserInfoDTO dto,SlActivitys vo)
    {
        Integer pageNum = vo.getPageNum();
        Integer pageSize = vo.getPageSize();
        if (StringUtils.isNotNull(pageNum) && StringUtils.isNotNull(pageSize))
        {
            PageHelper.startPage(pageNum, pageSize);
        }else {
            startPage();
        }
        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(HttpStatus.SUCCESS);
        rspData.setMsg("查询成功");
        if (dto==null){
            //未登录
            List<SlActivitys> slActivitys = slActivitysService.selectSlActivitysList(vo);
            SlActivitys laseAct=slActivitysService.selectNewActivity();
            if (laseAct!=null){
                rspData.setLastId(laseAct.getId());
            }else {
                rspData.setLastId(null);
            }
            rspData.setRows(slActivitys);
            rspData.setTotal(new PageInfo(slActivitys).getTotal());
        }else {
            //已登录，查询活动同时判断是否已报名
            List<SlActivitys> slActivitys = slActivitysService.selectSlActivitysAboutRecords(dto.getId());
            SlActivitys laseAct=slActivitysService.selectNewActivity();
            if (laseAct!=null){
                rspData.setLastId(laseAct.getId());
            }else {
                rspData.setLastId(null);
            }
            rspData.setRows(slActivitys);
            rspData.setTotal(new PageInfo(slActivitys).getTotal());
        }
        return rspData;
    }

    /**
     * 活动报名
     */
    @PostMapping("/slActivitySignUp")
    public ApiResult slActivitySignUp(@LoginUserInfo WxUserInfoDTO dto, @RequestBody SlActivitys vo){
        return slActivitysService.slActivitySignUp(dto,vo.getId());
    }

}
